package com.wangijun.yuan.admin.system.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.Db;
import com.wangijun.yuan.admin.base.CacheKey;
import com.wangijun.yuan.admin.base.CommonPage;
import com.wangijun.yuan.admin.system.domain.RoleMenu;
import com.wangijun.yuan.admin.system.domain.SysRole;
import com.wangijun.yuan.admin.system.mapper.SysRoleMapper;
import com.wangijun.yuan.admin.system.model.dto.RoleQryParam;
import com.wangijun.yuan.admin.system.model.dto.RoleUpdateParam;
import com.wangijun.yuan.admin.system.model.vo.RoleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@Service
@CacheConfig(cacheNames = CacheKey.role)
public class SysRoleService extends ServiceImpl<SysRoleMapper, SysRole> {


    public CommonPage<RoleVO> getList(RoleQryParam param) {
        Page<SysRole> page = this.lambdaQuery()
                .like(StrUtil.isNotBlank(param.getName()), SysRole::getName, param.getName())
                .page(param.page());
        return CommonPage.toPage(page, RoleVO.class);
    }

    @Cacheable(key = "#id")
    public SysRole get(Long id) {
        return this.getById(id);
    }

    @CacheEvict(key = "#param.id")
    public void update(RoleUpdateParam param) {
        SysRole sysRole = BeanUtil.copyProperties(param, SysRole.class);
        // 先删除
        Db.lambdaUpdate(RoleMenu.class)
                .eq(RoleMenu::getRoleId, sysRole.getId())
                .remove();
        for (Long menuId : param.getMenuIds()) {
            RoleMenu roleMenu = new RoleMenu();
            roleMenu.setRoleId(sysRole.getId());
            roleMenu.setMenuId(menuId);
            Db.save(roleMenu);
        }
        this.updateById(sysRole);
    }   


    /**
     * 根据角色Id获取菜单ID
     */
    public List<Long> getMenuIdById(Long roleId) {
        List<RoleMenu> list = Db.lambdaQuery(RoleMenu.class).eq(RoleMenu::getRoleId, roleId)
                .list();
        if (list.isEmpty()) {
            return List.of();
        }
        return list.stream().map(RoleMenu::getMenuId).collect(Collectors.toList());
    }



    @CacheEvict(key = "#id")
    @Transactional(rollbackFor = Exception.class)
    public void del(Long id) {
        Db.lambdaUpdate(RoleMenu.class).eq(RoleMenu::getRoleId, id).remove();
        this.removeById(id);
    }
    /**
     * 功能描述: 批量删除
     *
     * @author wlf 2024/8/23 14:32
     */
    public void delBatch(List<Long> ids) {
        for (Long id : ids) {
            SpringUtil.getBean(this.getClass())
                    .del(id);
        }
    }
}
